package com.qf.j2112.service.impl;

import com.github.pagehelper.PageHelper;
import com.qf.j2112.config.OrderConfig;
import com.qf.j2112.mapper.OrderMapper;
import com.qf.j2112.po.Order;
import com.qf.j2112.po.OrderItem;
import com.qf.j2112.service.OrderService;
import com.qf.j2112.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Transactional //添加事务管理
    @Override
    public boolean doOrderInvalid(Order order) {
        int i=orderMapper.orderFailed(order);
        if(i>-1){
            return true;
        }
        return false;
    }

    @Transactional //添加事务管理
    @Override
    public Order addOneOrder(Order order) {
        orderMapper.addOneOrder(order);
        if(order.getOrderId()!=0){
            return order;
        }
        return order;
    }

    @Override
    public List<Order> findAllOrder(int uid) {
        List<Order> allOrder = orderMapper.findAllOrder(uid);
        return allOrder;
    }

    @Override
    public List<OrderItem> findAllOrderItem(int oid) {
        List<OrderItem> allOrderItem = orderMapper.findAllOrderItem(oid);
        return allOrderItem;
    }

    @Override
    public int addOneItem(OrderItem orderItem) {
        int i = orderMapper.addOneItem(orderItem);
        return i;
    }

    @Override
    public List<Order> findAll(PageUtil pageUtil) {
        PageHelper.startPage(pageUtil.getPage(),pageUtil.getPageSize());
        return orderMapper.findAll();
    }

    @Override
    public int countOrder() {
        return orderMapper.countOrder();
    }

    @Override
    public List<Order> findUnpaidOrder() {
        return orderMapper.findUnpaidOrder();
    }

    @Override
    public int isOrderPay(String trade) {
        int orderPay = orderMapper.isOrderPay(trade);
        return orderPay;
    }

    @Transactional
    @Override
    public int getOutTicket(String cdk) {
        Order order = orderMapper.findOneByCdk(cdk);
        if(order.getIsPay()==OrderConfig.paid){
            int i=orderMapper.finishOrder(cdk);
            if(i>-1){
                return 1;
            }else {
                return 0;
            }
        }else {
            return -1;
        }
    }

    @Override
    public Order findOrderByCdk(String cdk) {
        return orderMapper.findOneByCdk(cdk);
    }
}
